﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using nSL.Config;
using System.Windows.Threading;
using System.Threading;
using nSL.Libs;

namespace nSL.UC
{
    /// <summary>
    /// Interaction logic for Video.xaml
    /// </summary>
    public partial class Video : UserControl
    {
        Border bdr;
        bool isMouseCaptured = false;
        public Video()
        {
            InitializeComponent();

            linearGauge.MouseLeftButtonDown += (s1, e1) =>
            {
                bdr = e1.OriginalSource as Border;
                isMouseCaptured = true;
                bdr.Cursor = Cursors.Hand;
                bdr.CaptureMouse();
            };
            linearGauge.MouseMove += (s3, e3) =>
            {
                if (isMouseCaptured)
                {
                    var pathlength = linearGauge.ActualWidth * linearGauge.XAxisLength;
                    var startposition = linearGauge.ActualWidth * linearGauge.XAxisLocation;
                    var offset = Convert.ToDouble((linearGauge.Maximum - linearGauge.Minimum) / pathlength);

                    var pixelvalue = Convert.ToDouble(offset * startposition);

                    linearGauge.Value = Convert.ToDouble((e3.GetPosition(linearGauge).X * offset) - pixelvalue + linearGauge.Minimum);
                    if (linearGauge.Value < linearGauge.Minimum)
                        linearGauge.Value = linearGauge.Minimum;
                    else if (linearGauge.Value > linearGauge.Maximum)
                        linearGauge.Value = linearGauge.Maximum;
                }
            };
            linearGauge.MouseLeftButtonUp += (s2, e2) =>
            {
                if (bdr != null)
                {
                    Console.WriteLine(linearGauge.Value);
                    mediaElement1.Position = new TimeSpan(0, 0, 0, 0, (int)linearGauge.Value);
                    bdr.ReleaseMouseCapture();
                    bdr.Cursor = Cursors.Arrow;
                    isMouseCaptured = false;
                }
            };
        }

        Common.LECTURE_INFO tagInfo;
        private string _fileURI;
        Thread thSetBar;

        public string FileURI
        {
            get { return _fileURI; }
            set { _fileURI = value; }
        }

        public Common.LECTURE_INFO TagInfo
        {
            get { return tagInfo; }
            set { tagInfo = value; }
        }

        private void UpdateVDOTime()
        {
            while (controlBar.Visibility == System.Windows.Visibility.Visible)
            {
                this.linearGauge.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { linearGauge.Value = mediaElement1.Position.TotalMilliseconds; }));
                Thread.Sleep(1000);
            }
        }

        bool IsPlaying()
        {
            var pos1 = mediaElement1.Position;
            System.Threading.Thread.Sleep(30);
            var pos2 = mediaElement1.Position;

            return (pos2 - pos1).TotalMilliseconds > 20;
        }

        private void imgStop_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            mediaElement1.Pause();
            if (Message.Confirm(cString.ID("video.stop")))
            {
                mediaElement1.Stop();
                UC.Lecture lecture = new Lecture();
                lecture.LectureInfo = this.TagInfo;
                WpfPageTransitions.PageTransition transition = new WpfPageTransitions.PageTransition();
                transition.TransitionType = WpfPageTransitions.PageTransitionType.Slide;
                transition.ShowPage(lecture);
                Window.GetWindow(this).Content = transition;
            }
            else
            {
                mediaElement1.Play();
            }
        }

        private void image1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            TimeSpan curTime = mediaElement1.Position;
            TimeSpan ts = TimeSpan.FromSeconds(10);
            mediaElement1.Position = curTime.Subtract(ts);
        }

        private void image3_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            TimeSpan curTime = mediaElement1.Position;
            TimeSpan ts = TimeSpan.FromSeconds(10);
            mediaElement1.Position = curTime.Add(ts);
        }

        private void image2_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            BitmapImage play = new BitmapImage();
            play.BeginInit();
            play.UriSource = new Uri("pack://application:,,,/nSL;component/Images/Player/play.png");
            play.EndInit();

            BitmapImage pause = new BitmapImage();
            pause.BeginInit();
            pause.UriSource = new Uri("pack://application:,,,/nSL;component/Images/Player/pause.png");
            pause.EndInit();

            if (IsPlaying())
            {
                mediaElement1.Pause();
                imgPlay.Source = play;
            }
            else
            {
                mediaElement1.Play();
                imgPlay.Source = pause;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            mediaElement1.Source = new Uri(FileURI);
            Console.WriteLine(FileURI);
            mediaElement1.Play();
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 0);
            mediaElement1.Position = ts;
        }

        private void trackBarEdit1_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            mediaElement1.Volume = (double)(trackBarEdit1.Value / 100.0);
        }

        private void mediaElement1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (controlBar.Visibility == System.Windows.Visibility.Visible)
            {
                controlBar.Visibility = System.Windows.Visibility.Hidden;
            }
            else
                controlBar.Visibility = System.Windows.Visibility.Visible;
        }

        private void controlBar_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            thSetBar = new Thread(new ThreadStart(UpdateVDOTime));
            thSetBar.Name = "SetPosition";
            thSetBar.IsBackground = true;
            if (controlBar.Visibility == System.Windows.Visibility.Visible)
            {
                thSetBar.Start();
            }
            else
            {
                thSetBar.Abort();
            }
        }

        private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
        {
            linearGauge.Maximum = mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        double _oldVolumn;
        private void imgVol_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (mediaElement1.Volume <= 0.0)
            {
                mediaElement1.Volume = _oldVolumn;
                trackBarEdit1.Value = _oldVolumn * 100;
            }
            else
            {
                _oldVolumn = (double)(trackBarEdit1.Value / 100.0);
                mediaElement1.Volume = 0.0;
                trackBarEdit1.Value = 0;
            }
        }

    }
}
